package com.ht.module.bus.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ht.abnormal.HtException;
import com.ht.config.dataAuth.DataScope;
import com.ht.constant.BusConstant;
import com.ht.constant.DicConstants;
import com.ht.module.bus.entity.TTransportationCompanyInformation;
import com.ht.module.bus.mapper.TTransportationCompanyInformationMapper;
import com.ht.module.bus.service.TTransportationCompanyInformationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ht.util.Pager;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 护苗专县承运企业信息表 服务实现类
 * </p>
 *
 * @author hejialun
 * @since 2024-07-29
 */
@Service
public class TTransportationCompanyInformationServiceImpl extends ServiceImpl<TTransportationCompanyInformationMapper, TTransportationCompanyInformation> implements TTransportationCompanyInformationService {

    @Override
    public Pager<TTransportationCompanyInformation> findPage(Pager<TTransportationCompanyInformation> pager, TTransportationCompanyInformation en) {
        QueryWrapper<TTransportationCompanyInformation> qw = getQW(en);
        return baseMapper.findPageAuth(pager,qw,null);
    }

    @Override
    public List<TTransportationCompanyInformation> findList(TTransportationCompanyInformation en) {
        QueryWrapper<TTransportationCompanyInformation> qw = getQW(en);
        qw.eq("state", DicConstants.State.ENABLE);
        return baseMapper.findListAuth(qw,null);
    }

    @Override
    public List<TTransportationCompanyInformation> findListAuth(TTransportationCompanyInformation en) {
        QueryWrapper<TTransportationCompanyInformation> qw = getQW(en);
        qw.eq("state", DicConstants.State.ENABLE);
        return baseMapper.findListAuth(qw,new DataScope());
    }

    @Override
    public Pager<TTransportationCompanyInformation> findPageAuth(Pager<TTransportationCompanyInformation> pager, TTransportationCompanyInformation en) {
        QueryWrapper<TTransportationCompanyInformation> qw = getQW(en);
        return baseMapper.findPageAuth(pager,qw,new DataScope().initOrder(pager));
    }

    @Override
    public void add(TTransportationCompanyInformation en) {
        //查询企业名称是否重复
        Integer i = baseMapper.selectCount(
                new QueryWrapper<TTransportationCompanyInformation>()
                        .eq("company_name", en.getCompanyName())
        );
        if(i>0){
            throw new HtException("企业名称重复，不可添加！");
        }
        baseMapper.insert(en);
    }

    @Override
    public void editById(TTransportationCompanyInformation en) {
        //查询企业名称是否重复
        Integer i = baseMapper.selectCount(
                new QueryWrapper<TTransportationCompanyInformation>()
                        .eq("company_name", en.getCompanyName())
                        .ne("company_code",en.getCompanyCode())
        );
        if(i>0){
            throw new HtException("企业名称重复，不可修改！");
        }
        baseMapper.updateById(en);
    }

    private QueryWrapper<TTransportationCompanyInformation> getQW(TTransportationCompanyInformation en){
        QueryWrapper<TTransportationCompanyInformation> qw=new QueryWrapper<>();
        if(StrUtil.isNotEmpty(en.getCompanyName())){
            qw.like("company_name",en.getCompanyName());
        }

        if(StrUtil.isNotEmpty(en.getState())){
            qw.eq("state",en.getState());
        }
        if(StrUtil.isNotEmpty(en.getOrganizationId())){
            qw.eq("organization_id",en.getOrganizationId());
        }
        if(StrUtil.isNotEmpty(en.getDistrictCode())){
            qw.eq("district_code",en.getDistrictCode());
        }
        qw.eq("del_flag", BusConstant.DEL_FLAG_N);
        qw.orderByDesc("create_date");
        return qw;
    }

}
